/**
 * Created with JetBrains PhpStorm.
 * User: xuheng
 * Date: 12-9-26
 * Time: 下午12:29
 * To change this template use File | Settings | File Templates.
 */

//清空上次查选的痕迹
editor.firstForSR = 0;
editor.currentRangeForSR = null;
var userLogin = $( '#frm_header #user_login', window.top.document ).val();
//是否区分大小写
function getMatchCase( id ) {
	//return $G(id).checked ? true : false;
	return false;
}

var underlineSwicth = window.top.localStorage.getItem( 'underlineSwicth' + userLogin );
var $underline_switch = $( '#underline_switch' );
if ( underlineSwicth == 0 ) {
	$underline_switch.removeClass( 'active' );
}
$underline_switch.css( 'display', 'inline-block' );


$( document ).on( 'click', '[bw-ui-switch]', function() {
	var $this = $( this );
	if ( $this.hasClass( 'active' ) ) {
		$this.removeClass( 'active' );
		window.top.localStorage.setItem( 'underlineSwicth' + userLogin, 0 );
	} else {
		$this.addClass( 'active' );
		$( '#underline_switch', window.top.synonym_frame.contentWindow.document ).addClass( 'active' );
		window.top.localStorage.removeItem( 'underlineSwicth' + userLogin );
	}
} );

// 查找下一个
/*$G("nextReplaceBtn").onclick = function(txt, dir, mcase) {
	var findtxt = $G("findtxt1").value,
		obj;
	if (!findtxt) {
		return false;
	}
	obj = {
		searchStr: findtxt,
		dir: 1,
		casesensitive: getMatchCase("matchCase1")
	};
	$G('replace-msg').innerHTML = '';
	if (!frCommond(obj)) {
		var bk = editor.selection.getRange().createBookmark();
		$G('replace-msg').innerHTML = lang.getEnd;
		editor.selection.getRange().moveToBookmark(bk).select();
	}
};*/
// 查找上一个
/*$G("preReplaceBtn").onclick = function(txt, dir, mcase) {
	var findtxt = $G("findtxt1").value,
		obj;
	if (!findtxt) {
		return false;
	}
	obj = {
		searchStr: findtxt,
		dir: -1,
		casesensitive: getMatchCase("matchCase1")
	};
	$G('replace-msg').innerHTML = '';
	if (!frCommond(obj)) {
		$G('replace-msg').innerHTML = lang.getStart;
	}
};*/
//替换
/*$G("repalceBtn").onclick = function() {
	var findtxt = $G("findtxt1").value.replace(/^\s|\s$/g, ""),
		obj,
		//replacetxt = $G("replacetxt").value.replace(/^\s|\s$/g, "");
		replacetxt = $('.synonyms_text .selected').text().replace(/^\s|\s$/g, "");

	if (!findtxt) {
		return false;
	}

	if (findtxt == replacetxt || (!getMatchCase("matchCase1") && findtxt.toLowerCase() == replacetxt.toLowerCase())) {
		return false;
	}
	obj = {
		searchStr: findtxt,
		dir: 1,
		casesensitive: getMatchCase("matchCase1"),
		replaceStr: replacetxt
	};
	frCommond(obj);
};*/
//全部替换
$G( "repalceAllBtn" ).onclick = function() {
	var findtxt = $G( "findtxt1" ).value.replace( /^\s|\s$/g, "" ),
		obj,
		//replacetxt = $G("replacetxt").value.replace(/^\s|\s$/g, "");
		replacetxt = $( '.synonyms_text .selected' ).text().replace( /^\s|\s$/g, "" );
	if ( !findtxt ) {
		return false;
	}
	if ( findtxt == replacetxt || ( !getMatchCase( "matchCase1" ) && findtxt.toLowerCase() == replacetxt.toLowerCase() ) ) {
		return false;
	}
	obj = {
		searchStr: findtxt,
		casesensitive: getMatchCase( "matchCase1" ),
		replaceStr: replacetxt,
		all: true
	};
	var num = frCommond( obj );
	if ( num ) {
		$G( 'replace-msg' ).innerHTML = lang.countMsg.replace( "{#count}", num );
	} else {
		$G( 'replace-msg' ).innerHTML = lang.nodataReplace;
	}
};

$G( 'queryBtn' ).onclick = function() {
	var findtxt = $G( "findtxt1" ).value.replace( /^\s+|\s+$/g, "" );
	if ( findtxt == '' ) return;
	var thisBtn = this;
	thisBtn.setAttribute( 'disabled', 'disabled' );
	thisBtn.innerText = lang.querying;
	$( '.synonyms_text .js_remove' ).remove();
	$( '.disabledBtn' ).prop( 'disabled', 'disabled' );
	$G( 'replace-msg' ).innerText = '';
	window.top.similarWordRequest( findtxt, function( data ) {
		thisBtn.removeAttribute( 'disabled' );
		thisBtn.innerText = lang.static.query;
		if ( data.ret == 0 && data.data && data.data.value && data.data.value.length ) {
			$( '.disabledBtn' ).removeProp( 'disabled' );
			for ( var i in data.data.value ) {
				$( ".synonyms_text" ).append( '<span class="js_remove synonyms_replace ' + ( i == 0 ? 'selected' : '' ) + '">' + data.data.value[ i ] + '</span>' )
			}
		} else {
			$( ".synonyms_text" ).append( lang.noData );
		}
	} );
}

$( '.synonyms_text' ).on( 'click', '.synonyms_replace', function() {
	$( this ).addClass( 'selected' ).siblings().removeClass( 'selected' );
} );

//执行
var frCommond = function( obj ) {
	return editor.execCommand( "searchreplace", obj );
};